<?php
/*
 * Created on 17-Jan-2005
 *
 */

using('System.Data');

class DataModule extends TModule
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		require("config.php");
		$this->Database->setDataSourceName($dsn);
	}
	
	public function hasGrants()
	{
		$userid = $this->User->getId();
		
		$result = $this->Database->Execute("SELECT * FROM users WHERE id='$userid'");
		$fields = $result->fields;
		
		if($fields['repositorygrants']>0)
			return true;
		
		if($this->User->isAdmin())
			return true;
		
		return false; 		
	}
	
	public function isTaken($name)
	{
		$result = $this->Database->Execute("SELECT * FROM repositories WHERE name='$name'");
		return $result->RecordCount()>0;
	}

	public function ownsRepositories()
	{
		$userid = $this->User->getId();
		$result = $this->Database->Execute("SELECT * FROM repositories WHERE ownerid='$userid'");
		return $result->RecordCount()>0;
	}
	
	public function createRepository($userid, $name)
	{
		require "config.php";
		
		$rname = strtolower($name);
		$dir = $svn_repos_loc.DIRECTORY_SEPARATOR.$rname;
		
		if(file_exists($dir))
		{
			error_log("Can't make repository, $dir already exists.'");
			return;
		} else {
			//Create repository
			mkdir($dir);
			exec($svnadmin_cmd." create ".$dir);
						
			//Add repository into database
			$result = $this->Database->Execute("INSERT INTO repositories (id, name, ownerid) VALUES (null, '$rname', '$userid')");
			
			//Rebuild accessfile
			$this->rebuildAccessFile();		
		}
		
	}
	
	public function deleteRepository($id)
	{				
		require("config.php");
		
		//File removal
		$result = $this->Database->Execute("SELECT name FROM repositories WHERE id='$id'");
		$name = $result->fields['name'];				
		if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
 			exec("rmdir /q /s " . $svn_repos_loc.DIRECTORY_SEPARATOR.$name);
		} else {
			exec("rm -rf ".$svn_repos_loc.DIRECTORY_SEPARATOR.$name);	
		}
		
		//User privileges
		$result = $this->Database->Execute("DELETE FROM userprivileges WHERE repositoryid='$id'");
		
		//Group privileges
		$result = $this->Database->Execute("DELETE FROM groupprivileges WHERE repositoryid='$id'");
		
		//Database removal
		$result = $this->Database->Execute("DELETE FROM repositories WHERE id='$id'");
		
		//Accessfile recreation
		$this->rebuildAccessFile();
		
	}	
	
	public function renameRepository($id, $newname)
	{
		require("config.php");

		//Check if new name is unique; repos doesn't already exist
		$results = $this->Database->Execute("SELECT * FROM repositories WHERE name='$newname'");
		if($results->RecordCount()>0 || file_exists($svn_repos_loc.DIRECTORY_SEPARATOR.$newname) )
		{
			error_log("Can't rename repository to already existing name!");
			exit(-1);
		}
				
		//Retrieve old name
		$results = $this->Module->Database->Execute("SELECT name FROM repositories WHERE id='$id'");		
		$oldname = $results->fields['name'];
		
		//Rename in database
		$this->Module->Database->Execute("UPDATE repositories SET name='$newname' WHERE id='$id'");
		
		//Rename folder
		if(!rename($svn_repos_loc.DIRECTORY_SEPARATOR.$oldname, $svn_repos_loc.DIRECTORY_SEPARATOR.$newname))
		{
			error_log("Error renaming repository!");
			exit(-1);
		}
		
		$this->rebuildAccessFile();			
		
	}
	
	public function changeRepositoryOwner($reposid, $newownerid)
	{
		$this->Database->Execute("UPDATE repositories SET ownerid='$newownerid' WHERE id='$reposid'");

		$this->rebuildAccessFile();
	}
	
	public function getUsername($id)
	{
		//User 0 is the '*' referring to all users in the svn access file
		if($id==0) return "*"; 
		
		$user = $this->Database->Execute("SELECT * FROM users WHERE id='$id'");
		if($user)
			return $user->fields['name'];
		else
			return null;
	}

	public function getGroupname($id)
	{
		$group = $this->Database->Execute("SELECT * FROM groups WHERE id='$id'");
		if($group)
			return $group->fields['name'];
		else
			return null;
	}

	public function getUserId($name)
	{
		$id = $this->Database->Execute("SELECT * FROM users WHERE name='$name'");
		if($id)
			return $id->fields['id'];
		else
			return null;
	}

	public function getGroupId($name)
	{
		$id = $this->Database->Execute("SELECT * FROM groups WHERE name='$name'");
		if($id)
			return $id->fields['id'];
		else
			return null;
	}
		
	public function getRepositoryname($id)
	{
		$repos = $this->Database->Execute("SELECT * FROM repositories WHERE id='$id'");
		if($repos)
			return $repos->fields['name'];
		else
			return null;
	}

	public function removeUserPrivileges($id)
	{
		$results = $this->Database->Execute("DELETE FROM userprivileges WHERE id='$id'");
		$this->rebuildAccessFile();	
	}

	public function removeGroupPrivileges($id)
	{
		$results = $this->Database->Execute("DELETE FROM groupprivileges WHERE id='$id'");
		$this->rebuildAccessFile();	
	}

	public function addUserPrivileges($userid, $repositoryid, $path, $access)
	{
		$results = $this->Database->Execute("INSERT INTO userprivileges (id, userid, repositoryid, path, access) VALUES (null, '$userid', '$repositoryid', '$path', '$access')");
		$this->rebuildAccessFile();
		
	}	

	public function addGroupPrivileges($groupid, $repositoryid, $path, $access)
	{
		$results = $this->Database->Execute("INSERT INTO groupprivileges (id, groupid, repositoryid, path, access) VALUES (null, '$groupid', '$repositoryid', '$path', '$access')");
		$this->rebuildAccessFile();
		
	}	

	/*
	 * Access:
	 * 0 = no access
	 * 1 = r 
	 * 2 = w 
	 * 3 = rw
	 */
	
	function rebuildAccessFile()
	{
		require_once("./svnmanager/library/class.accessfile.php");
		$accessfile = new AccessFile();
		$accessfile->createFromDatabase();
	}
	
	function updateGrants($userid, $nr)
	{
		$this->Module->Database->Execute("UPDATE users SET repositorygrants='$nr' WHERE id='$userid'");			
	}
	
	function getGrants($userid)
	{
		$result = $this->Module->Database->Execute("SELECT repositorygrants FROM users WHERE id='$userid'");
		return $result->fields['repositorygrants'];
	}
	
}

?>